<?php
//$proId=$_REQUEST['products'];
require_once 'includes/class.phpmailer.php';

$itemId=$_REQUEST['wholeproducts'];
$sqlSelItem="SELECT * FROM tbl_items WHERE item_id='$itemId'";
$rstSelItem=mysql_query($sqlSelItem);
$rowSelItem=mysql_fetch_array($rstSelItem);
$proId=$rowSelItem['product_id'];	
$productOwner=$rowSelItem['seller_id'];
$productDesc=$rowSelItem['product_description'];
$productCategory=$rowSelItem['category_id'];
$productPrice=$rowSelItem['price'];

$sqlSelProd="SELECT * FROM tbl_wholeseller_product WHERE product_id='".$_REQUEST['wholeproducts']."'";
$rstSelProd=mysql_query($sqlSelProd);
$rowSelProd=mysql_fetch_array($rstSelProd);
$owner_id=$rowSelProd['posted_by'];
$productCategory=$rowSelProd['category_id'];
$productOOwner=$rowSelProd['posted_by'];

$productName=$rowSelProd['product_name'];	
$postedDate=$rowSelProd['posted_date'];
$proId=$rowSelProd['product_id'];	
$productDesc=$rowSelProd['product_description'];
$productCategory=$rowSelProd['category_id'];
$productPrice=$rowSelProd['product_price'];

$pimage = getWholeProductImage($itemId,'p');
 //echo $pthumb;
if($pimage['thumb']!="") {
	$productThumbnail = $pimage['thumb'];
 }  else {
	$productThumbnail="images/no_image.gif";	
 } 
		  
$catInfo=getCategory($productCategory);

$shipping=0;
$sales_tax=0;
?>


<style type="text/css">
	span#add-wishlist { color:#0000CC; padding:5px; font-size:12px; }	
</style>



<table width="100%" border="0">
  <tr>
    <td class="contentHeader"><h2>Products</h2></td>
  </tr>
  <tr>
    <td><span id='listed-in'>Listed in: <a href='index.php?cat=<?php echo $catInfo['categoryId']; ?>'><?php echo $catInfo['categoryName']; ?></a> >> <a href='index.php?cat=<?php echo $catInfo['subcategoryId']; ?>'><?php echo $catInfo['subcategoryName']; ?></a></span>
	   <br style="clear:both;"><br>
	   
	   
	   		<?php
				if(!empty($_POST['hidBargain'])) {
					if($_SESSION['user_id_sess']!="") { 
						$askingAmount = $_POST['asking_price'];
						$offerAmount = $_POST['offer_price'];
						$comment = $_POST['bargain_comment'];
						
						if($offerAmount=="") {
							echo "<div id='err-msg'>Enter offer amount.</div>"; 
						}					
						else {
							if($_POST['hidBargain']=="add") {							
							$sqlInsBargain="INSERT INTO tbl_bargain set item_id='$itemId', 
																		asking_amount='$askingAmount', 
																		owner_id='$owner_id',
			  															user_id='$userId', 
																		user_type='3', 
																		offer_amount='$offerAmount', 
																		comment='$comment', 
																		bargain_type=1,
																		status='offer'";
							$rstInsBargain=mysql_query($sqlInsBargain);								
							$add_rst=mysql_insert_id();
									if($add_rst) { 
									
									$emailContent="You are receiving this message because (".$_SESSION['user_name'].") has made an offer towards (".$_POST['pname'].")<br><p>Offer: $".number_format($offerAmount,2)."<p><p>".$comment."</p>";

									mysql_query("insert into tbl_messages(senderid,recieverid,productid,subject,message,posteddate,status,type,itemid,section,bargain_id) values('".$_SESSION['user_id_sess']."','".$_POST['recieverid']."','".$proId."','Offer Recieved','".addslashes($emailContent)."','".date("Y-m-d")."','1','Bargain','".$itemId."','WS','".$add_rst."')");



 $mailmsg="<table width='100%' border='0' style='font-family:Verdana, Arial, Helvetica, sans-serif;font-size:12px; line-height:25px'>
 
  <tr>
    <td height='32' colspan='2' style='font-family:Verdana, Arial, Helvetica, sans-serif;font-size:11px;'>".$emailContent."</td>
  </tr>
  
 
</table>
";
		$mail = new PHPMailer(); // defaults to using php "mail()"
	  	$mail->MsgHTML($mailmsg);
		$mail->From       = "mail@swapen.com";
		$mail->FromName   = "Swapen.com";
		$mail->Subject    = "Offer Recieved";
		//$mail->AddAddress("suggestions@swapen.com", $firstName);
		$mail->Send();
									
									
									echo "<div id='suc-msg'>Bargain placed successfully.</div>"; }
									else { echo "<div id='err-msg'>Error on placing your bargain.</div>"; }						
							}						
						}
					} else {
						echo "<script type='text/javascript'> window.location='index.php?signin&url=bargain'; </script>";
					}
					
				}	
			?>
	   
<script language="javascript1.2">
function addtocart(id,name,amt,sh,q,tax)
{
	location.href="addtocart_merchant.php?id="+id +"&name="+name+"&amt="+amt+"&shipping="+sh+"&qty="+q+"&tax="+tax;
}
function addtocart_buy(id,name,amt,sh,q,tax)
{
	if(isNaN(q)==true || q=='' || parseInt(q)<=0)
	{
		alert("Please enter the quantity you want to buy");
	}
	else
	{
		location.href="addtocart_merchant.php?id="+id +"&name="+name+"&amt="+amt+"&shipping="+sh+"&qty="+q+"&tax="+tax;
	}
}
</script>
<div class="contentHeader"><strong><?php if($_SESSION['msg']!='') echo $_SESSION['msg'];$_SESSION['msg']='';?></strong></div>	  
	   <form name="frmProductInfo" id="frmProductInfo" method="post">
	   <input type="hidden" name="hidBargain" id="hidBargain">
	   <div style="border:1px solid #CCC;">
         <table width='100%' border='0' align='center' cellspacing='0'>
           <tr>
             <td width="16%" rowspan="2" style='vertical-align:middle; padding:5px;background-color:#F5F5F5; text-align:center'><img src="<?php echo $productThumbnail; ?>" /> <div>
             </div></td>
             <td style='vertical-align:top; padding:5px;'><?php echo "$productName";?> </td>
             <td width="13%" style='vertical-align:top; padding:5px;'><strong>Price </strong> <br />
                 <br />
               $<?php echo number_format($productPrice,2); ?> </td>
             <td width="19%" style='vertical-align:top; padding:5px;'></td>
           </tr>
           <tr>
             <td height="25"  style='padding:5px;'><?php
			  //echo checkShippingDetails($sellingType);
			  ?>             </td>
             <td colspan="2" align="center" style='vertical-align:top; padding:5px;'><?php if($_SESSION['user_id_sess']=="") { $usr=0; } else { $usr=$_SESSION['user_id_sess'];} ?>
             <?php if(($_SESSION['user_id_sess']!=$productOwner) && (($_SESSION['user_type_sess']!=1) || ($_SESSION['user_type_sess']!=3))) { ?>
                 <a href="javascript:void(0);" onclick="funAddWholeList('<?php echo $itemId; ?>','<?php echo $_SESSION['user_id_sess']; ?>','add-wishlist');">Add to wishlist</a>
                 <?php } ?>
                 <br />
               <br />
               <span id="add-wishlist"></span> </td>
           </tr>
           <tr>
             <td height="25" colspan="2" ><a href="javascript:void(0);" onclick="openWindow('item_whole_viewer.php?prod=<?php echo $proId; ?>&type=i','580','650');"><?php if(getWholeImageCount($proId)!=0) echo getWholeImageCount($proId)." images"  ?></a></td>
             <td colspan="2" align="center" style='vertical-align:top; padding:5px;'>&nbsp;</td>
           </tr>
         </table>
	     </div>
	   <br>
	   <div style="border:1px solid #CCC;"> 			
	   <table width="100%" border="0" align="center">
          
		   <tr>
             <td width="47%" valign="top">              
             	<div style="padding:15px;" align="justify"> 
					<strong>Description </strong>
					<p style="text-indent:20px; line-height:22px;">
						<?php echo nl2br($productDesc); ?>
					</p>
				</div>
			 </td>
             <td width="53%" valign="top"><table  border="0" align="center" width="100%">              
               <tr>
                 <td width="93%" height="28" colspan="2" class="uline">
				 	<table align="center" width="90%" border="0">
						<tr>
							<td height="33" align="center">
								<input type="text" size="7" name="buy_amount" id="buy_amount">
								<input type="button" value="Buy" name="btnBuy" onclick="return addtocart_buy('<?php echo $proId;?>','<?php echo $productName;?>','<?php echo $productPrice;?>','<?php echo $shipping;?>',document.getElementById('buy_amount').value,'<?php echo $sales_tax;?>');">									
							</td>
						</tr>
						<tr>
						  <td height="32" align="center"><input type="button" value="Add to Cart" onclick="return addtocart('<?php echo $proId;?>','<?php echo $productName;?>','<?php echo $productPrice;?>','<?php echo $shipping;?>',1,'<?php echo $sales_tax;?>');"></td>
					  </tr>
						<tr>
						  <td height="27" align="center"><strong>Bargain</strong></td>
					  </tr>
						
						<tr>
						  <td align="center"><table width="84%"  border="0">
                            <tr>
                              <td width="50%" align="center">Asking price </td>
                              <td width="50%" align="center">Your offer </td>
                              </tr>
                            <tr>
                              <td height="38" align="center"><input type="text" readonly size="7" id="asking_price" name="asking_price" value="<?php echo $productPrice; ?>"></td>
                              <td align="center"><input type="text" size="7"  name="offer_price" id="offer_price"></td>
                              </tr>
                            <tr>
                              <td colspan="2" align="center">Include Comment</td>
                            </tr>
                            <tr>
                              <td colspan="2" align="center"><textarea name="bargain_comment" id="bargain_comment" style="height:150px; width:250px; "></textarea>
								<input type="hidden" name="recieverid" value="<?php echo $productOOwner;?>"/>
                                <input type="hidden" name="pname" value="<?php echo $productName;?>"/></td>
                            </tr>
                            <tr>
                             <td colspan="2" align="center"><input type="button" <?php if($userId==$productOwner) { echo " disabled "; } ?> name="btnCommit" value="Commit" onClick="funBargain();"></td>
                            </tr>
                          </table></td>
					  </tr>
					  
					</table>
				 </td>
               </tr>
             </table></td>
           </tr>          
         </table>
		 </div>
	  </form>
	  <br>
	  <div style="border:1px solid #CCC;"> 
	  		<table width="100%" align="center" border="0">
				<tr>
					<td><strong>Questions and Offers</strong></td>
				</tr>
				<tr>
					<td>
						<?php 
						if(!empty($_POST['hidSellerReply'])) { 
								$parentBargain = $_POST['hidSellerReply'];
								if($_POST['hidType']=="add") {								
									$key = $_POST['hidReplyFor'];								
									$askAmt = $_POST['ask_amount'];
									$buyAmt = $_POST['buyer_amount'];
									$selCmt = $_POST['seller_comment'];
									if($askAmt[$key]=="") {
										echo "<div id='err-msg'>Enter asking amount.</div>";
									}
									
									else {
										$sqlAddReply="INSERT INTO tbl_bargain set product_id='$proId',
																		asking_amount='$askAmt[$key]', 
																		user_id='$userId', 
																		parent_id='$parentBargain', 
																		user_type='1', 
																		offer_amount='$buyAmt[$key]', 
																		bargain_type=1,
			  															comment='$selCmt[$key]', 
																		status='reply'";
										$rstAddReply=mysql_query($sqlAddReply);		
										$addReply=mysql_insert_id();								
										if($addReply) { echo "<div id='suc-msg'>Your reply posted successfully.</div>"; }
										else { echo "<div id='err-msg'>Error on placing your reply.</div>"; }		
									}
								 }
								 elseif($_POST['hidType']=="delete") {
								 $sqlDelReply="DELETE FROM tbl_bargain where bargain_id='".$_POST['hidSellerReply']."'";
									$rstDelReply=mysql_query($sqlDelReply);									
									if($rstDelReply) { echo "<div id='suc-msg'>Your reply deleted.</div>"; }
									else { echo "<div id='err-msg'>Error on deleting your reply.</div>"; }
								 }	 
						  }
						?>
						<form name="frmProductOffer" action="" method="post" id="frmProductOffer">
						<input type="hidden" name="hidSellerReply" id="hidSellerReply">
						<input type="hidden" name="hidReplyFor" id="hidReplyFor">
						<input type="hidden" name="hidType" id="hidType">
						<?php
							include_once("product_offer.php");
						?>
						</form>
					</td>
				</tr>
			</table>
	  </div>
    </td>
  </tr>
</table>		 